package com.egdigital.allzic.data.json;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.egdigital.allzic.data.object.Department;

public class JsonParserDepartment {

	// log
	private static final String			TAG					= JsonParserDepartment.class.getSimpleName();
	// data
	private static final String			TAG_ID				= "id";
	private static final String			TAG_TITLE			= "title";
	private RequestQueue				mRequestQueue;
	private static JsonParserDepartment	mJsonParser;
	private Department					mDept;
	private JSONArray					mJsonArrayCountry	= null;
	private JsonCallBack				mJsonCallback;

	public static JsonParserDepartment getInstance() {
		if (mJsonParser == null) {
			return new JsonParserDepartment();
		} else {
			return mJsonParser;
		}
	}

	public void initAndStartRequestQueue() {
		mRequestQueue = VolleyManager.getRequestQueue();
		mRequestQueue.start();
	}

	public void stopRequestQueue() {
		mRequestQueue.stop();
	}

	public void extractJsonObjectData(String url) {
		JsonArrayRequest request = new JsonArrayRequest(url, createMyReqSuccessListener(), createMyReqErrorListener());
		request.setTag(this);
		mRequestQueue.add(request);
		mJsonCallback.downloadStarted();
	}

	protected Response.Listener<JSONArray> createMyReqSuccessListener() {
		return new Response.Listener<JSONArray>() {

			public void onResponse(JSONArray jsonArray) {
				List<Department> list = new ArrayList<Department>();
				try {
					mJsonArrayCountry = jsonArray;
					for (int i = 0; i < mJsonArrayCountry.length(); i++) {
						mJsonCallback.downloadPercent(((i + 1) * 100) / mJsonArrayCountry.length());
						JSONObject jsonObject = mJsonArrayCountry.getJSONObject(i);
						mDept = new Department(jsonObject.getString(TAG_ID), toAlphabetFirstLetter(jsonObject.getString(TAG_TITLE)));
						list.add(mDept);
					}
				} catch (Exception ex) {
				}
				mJsonCallback.downloadCompleted(list);
			}
		};
	}

	protected Response.ErrorListener createMyReqErrorListener() {
		return new Response.ErrorListener() {

			public void onErrorResponse(VolleyError error) {
				mJsonCallback.downloadEndedWithError(error);
			}
		};
	}

	public void setJsonCallBack(JsonCallBack callback) {
		mJsonCallback = callback;
	}

	private String toAlphabetFirstLetter(String str) {
		String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);
		Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
		String resultString = pattern.matcher(nfdNormalizedString).replaceAll("");
		resultString = resultString.substring(0, 1).toUpperCase() + resultString.substring(1).toLowerCase();
		return resultString;
	}

	public String method(String str) {
		if (str.length() > 0 && str.charAt(str.length() - 1) == 'x') {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}
}
